package com.eqdd.yiqidian.http;


import android.app.Activity;

import com.eqdd.yiqidian.base.BaseActivity;
import com.eqdd.yiqidian.base.loading.INetLoadingView;
import com.eqdd.yiqidian.base.loading.waitdialog.DialogHelper;
import com.eqdd.yiqidian.utils.ToastUtil;
import com.lzy.okgo.model.Response;
import com.lzy.okgo.request.Request;

import java.net.SocketTimeoutException;
import java.net.UnknownHostException;

/**
 * Created by lvzhihao on 17-7-4.
 */

public abstract class DialogCallBack<T> extends JsonCallBack<T> {

    private String msg;
    private INetLoadingView netLoadingView;

    private Activity activity;

    public DialogCallBack(Activity activity) {
        super();
        netLoadingView = new DialogHelper(activity);
        this.activity = activity;
    }

    private DialogCallBack(Activity activity, String msg) {
        netLoadingView = new DialogHelper(activity);
        this.msg = msg;
    }

    @Override
    public void onStart(Request<T, ? extends Request> request) {
        super.onStart(request);
        if (msg == null){
            netLoadingView.showLoading();
        }else {
            netLoadingView.showLoading(msg);
        }
    }

    @Override
    public void onFinish() {
        super.onFinish();
        if (msg == null){
            netLoadingView.hideLoading();
        }else {
            netLoadingView.hideLoading(msg);
        }

    }

    @Override
    public void onError(Response<T> response) {
        super.onError(response);
        if (response.getException() instanceof SocketTimeoutException) {
            ToastUtil.showShort("网络连接超时");
        } else if (response.getException() instanceof UnknownHostException) {
            ToastUtil.showShort("网络不可达");
        }
        activity.finish();

    }
}
